﻿using DotNetMVC.Common;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace DotNetMVC.Website.Areas.WebAdmin.Controllers
{
    public class UploaderController : Controller
    {
        // GET: WebAdmin/Uploader
        public ActionResult UploadImage(HttpPostedFileBase file, string dirName = "images")
        {
            //限制上传大小
            int maxSize = 2 * 1024 * 1024;
            //允许上传文件
            string[] allowFiles = { ".png", ".jpg", ".jpeg", ".gif", ".bmp" };
            return Json(BaseUploadFile(file, dirName, maxSize, allowFiles));
        }

        public ActionResult UploadFile(HttpPostedFileBase file, string dirName= "files")
        {
            //限制上传大小
            int maxSize =50 * 1024 * 1024;
            //允许上传文件
            string[] allowFiles = { ".png", ".jpg", ".jpeg", ".gif", ".bmp",
".flv", ".swf", ".mkv", ".avi", ".rm", ".rmvb", ".mpeg", ".mpg",
".ogg", ".ogv", ".mov", ".wmv", ".mp4", ".webm", ".mp3", ".wav", ".mid",
".rar", ".zip", ".tar", ".gz", ".7z", ".bz2", ".cab", ".iso",
".doc", ".docx", ".xls", ".xlsx", ".ppt", ".pptx", ".pdf", ".txt", ".md", ".xml"};
            return Json(BaseUploadFile(file, dirName, maxSize, allowFiles));
        }

        /// <summary>
        /// 上传文件
        /// </summary>
        /// <param name="file">上传控件</param>
        /// <param name="dirName">上传到哪个目录</param>
        /// <param name="maxSize">限制上传大小</param>
        /// <param name="allowFiles">允许上传文件格式</param>
        /// <returns></returns>
        public AjaxResult BaseUploadFile(HttpPostedFileBase file, string dirName, int maxSize, string[] allowFiles)
        {
            if (file == null)
            {
                return new AjaxResult { Status = "error", ErrorMsg = "获取不到文件资源" };
            }

            if (file.ContentLength <= 0)
            {
                return new AjaxResult { Status = "error", ErrorMsg = "文件大小为0" };
            }

            //限制上传大小
            if (file.ContentLength > maxSize)
            {
                return new AjaxResult { Status = "error", ErrorMsg = "文件大小不能超过" + (maxSize / (1024 * 1024)) + "M" };
            }

            //获取文件后缀名
            string ext = Path.GetExtension(file.FileName);

            //允许上传文件
            bool allowFilesFlg = false;
            foreach (string allowFile in allowFiles)
            {
                if (ext == allowFile)
                {
                    allowFilesFlg = true;
                    break;
                }
            }
            if (!allowFilesFlg)
            {
                return new AjaxResult { Status = "error", ErrorMsg = "不支持" + ext + "格式" };
            }

            //month月，minute
            string md5 = CommonHelper.CalcMD5(file.InputStream);
            string path = "/upload/" + dirName + "/" + DateTime.Now.ToString("yyyyMMdd") + "/" + md5 + ext;// /upload/2017/07/07/afadsfa.jpg
            string fullPath = HttpContext.Server.MapPath("~" + path);//d://22/upload/2017/07/07/afadsfa.jpg
            new FileInfo(fullPath).Directory.Create();//尝试创建可能不存在的文件夹

            file.InputStream.Position = 0;//指针复位
            file.SaveAs(fullPath); //SaveAs("d:/1.jpg");

            return new AjaxResult{ Status = "ok",Data = new { path = path } };
        } 
    }
}